import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/pages/account/components/input_item.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/views/home/components/home_scan.dart';

class SearchHeader extends StatelessWidget {
  final AppColors theme;
  final GlobalKey<InputItemState> inputItemKey;
  final String searchKeyword;
  final Function(String value) onChanged; 
  final VoidCallback searchKeywordClear;
  final Function(String value) onSearchSubmit;
  final VoidCallback inputValueSubmit;
  const SearchHeader({
    super.key, 
    required this.theme, 
    required this.inputItemKey, 
    required this.searchKeyword, 
    required this.onChanged, 
    required this.searchKeywordClear,
    required this.onSearchSubmit, 
    required this.inputValueSubmit
  });

  static final navigation = Get.find<NavigationController>();

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Padding(
          padding: EdgeInsets.only(right: 6),
          child: GestureDetector(
            onTap: () {
              navigation.back();
            },
            child: Icon(
              Icons.arrow_back_ios,
              size: 14,
            ),
          ),
        ),
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 8, vertical: 6),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(16),
              color: theme.subjectMix2,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.min,
              children: [
                Padding(
                  padding: EdgeInsets.only(right: 8),
                  child: Icon(
                    Icons.search_sharp,
                    size: 16,
                  ),
                ),
                Expanded(
                  child: InputItem(
                    key: inputItemKey,
                    theme: theme,
                    placeholder: '请输入试题/面经关键字',
                    statusText: searchKeyword, // 显示当前关键词
                    onChanged: (value) {
                      onChanged(value);
                    },
                    onSubmitted: (value) {
                      inputItemKey.currentState?.unfocus(); // 收起键盘
                      // 用户提交时触发搜索
                      onSearchSubmit(value);
                    },
                  ),
                ),
                if (searchKeyword.isNotEmpty)
                  GestureDetector(
                    onTap: () {
                      searchKeywordClear();
                      inputItemKey.currentState?.clear(); // 清空输入框
                    },
                    child: Icon(
                      Icons.close,
                      size: 12,
                      color: theme.contrastMix3,
                    ),
                  )
                else
                  HomeScan(theme: theme),
              ],
            ),
          ),
        ),
        Padding(
          padding: EdgeInsets.only(left: 6),
          child: GestureDetector(
            onTap: () {
              inputItemKey.currentState?.unfocus(); // 收起键盘
              inputValueSubmit();
            },
            child: Text(
              "搜索",
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.bold,
                color: theme.contrastPure,
              ),
            ),
          ),
        )
      ],
    );
  }
}